<?php
declare (strict_types = 1);

namespace app\common\model;
use app\common\controller\Config;
use addons\Target\facade\Target;
class Mutual extends Base
{

    protected $name = 'mutual';
    protected $type = [
        'confirm_time' => 'timestamp',
    ];

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile,tag');
    }

    public function getStatusAttr($value)
    {
        return match ($value) {
            1 => '待审核',
            2 => '已通过',
            3 => '已驳回',
            default => ''
        };
    }
    
    public static function _save($param, $logs)
    {

        parent::startTrans();
        try {
            if (!isset($param['status'])) return ['code' => -1, 'msg' => '请选择审核类型'];

            $info = parent::find($param['id']);
            if ($info->isEmpty()) return ['code' => -1, 'msg' => '审核失败'];
            if ($info->getData('status') != 1) {
                return ['code' => -1, 'msg' => '此笔记录已审核，请勿重复审核！'];
            }
            if ($param['status'] == 2) {
                $msg = '通过互助申请,标识ID【' . $param['id'] . '】会员【' . $info->user->code . '】';
                $ress= self::getCrowdMoney($info->uid,$info->money);
                User::isValid();
                // print_r($ress);die;
                if($ress['code'] != 1){
                    return ['code' => -1, 'msg' => $ress['msg']];
                }
            } else {
                $msg = '驳回互助申请,标识ID【' . $param['id'] . '】会员【' . $info->user->code . '】';
            }

            $info->status = $param['status'];
            if ($param['status'] != 2) {
                $info->reject_msg = $param['reject_msg'];
            }
            $info->admin_id = $logs['admin']->id;
            $info->admin_name = $logs['admin']->admin_name;
            $info->confirm_time = time();
            $info->save();
            Logs::adds($msg, $logs['admin']->id, $logs['admin']->admin_name);
            parent::commit();
            return ['code' => 1, 'msg' => '已审核'];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 添加互助
     */

    public static function add($param, $user): array
    {
        try {
            if($user->is_valid !=1){
                return ['code' => -1, 'msg' => '不是有效会员不可参与互助！'];
            }
            $info  =  parent::where(['uid'=>$user->id])->find(); 
            $data = [
                'uid'     => $user->id,
                'rname'   => $param['rname'],
                'sex'     => $param['sex'],
                'idcard'  => $param['idcard'],
                'money'   => $param['money'],
                'info_img'   => $param['info_img'],
                'hospital_in_img' => $param['hospital_in_img'],
                'proof_img' => $param['proof_img'],
                'health_img' => $param['health_img'],
                'hospital_to_img' => $param['hospital_to_img'],
                'note' => $param['note'],
            ];
            if(empty($info)){
                parent::create($data);
                return ['code' => 1, 'msg' => '申请已提交，请耐心等待审核', 'url' => url('help/index')->build()];
            }else{
                if ($info->getData('status') == 3) {
                    $data['status'] = 1;
                    parent::where(['id' => $info->id])->update($data);
                    return ['code' => 1, 'msg' => '申请已提交，请耐心等待审核','url' => url('help/index')->build()];
                } else if ($info->getData('status') == 1) {
                    return ['code' => 1, 'msg' => '您已提交申请，请耐心等待审核'];
                } else {
                    return ['code' => 1, 'msg' => '您已申请了互助基金，不可重复申请'];
                }
            }
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     *互助众筹
     */
    public static function getCrowdMoney($uid,$money)
    {
        $sys = Config::lists();
        try{
            $count =  Assets::where('uid','<>',$uid)->where('help_money','>=',$sys['help_up'])->column('uid');
            $num = round($money/count($count),2);
            if($num>$sys['help_share']){
                return ['code' => -1, 'msg' => '每人分摊金额超过'.$sys['help_share'].'美金'];
            }
            foreach($count as $v){
                Target::Account()->basics($v, 'help_money', $num, '互助基金分摊', '-')
                    ->createNormal($uid);
            }
            return ['code' => 1, 'msg' => '可以众筹'];
        }catch(\Exception $e){
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

}
